﻿using System;
using System.Collections;
using System.Threading.Tasks;
using RAP.Framework.Libary.JSONSocket;
using RAP.Framework.Libary.Utils;

namespace RAP.Framework.Libary.DebugConsole
{
  /// <summary>
  /// 远程控制台
  /// </summary>
  public class RemoteConsole : IDebugConsole
  {
    protected SocketJSON _serverSocketJSON = new SocketJSON();

    public RemoteConsole(string serverIP, int port)
    {
      _serverSocketJSON.ServerIP = serverIP;
      _serverSocketJSON.Port = port;
      _serverSocketJSON.StartListen();
    }

    public virtual ConsoleColor BackgroundColor { get; set; }

    public virtual IDictionary Config { get; set; }

    public virtual ConsoleColor ForegroundColor { get; set; }

    public virtual string Source { get; set; }

    public virtual void Dispose()
    {
      if (_serverSocketJSON != null)
      {
        _serverSocketJSON.Close();
        _serverSocketJSON.Dispose();
        _serverSocketJSON = null;
      }
    }

    public virtual IDebugConsole Write(string msg)
    {
      Task.Run(() =>
      {
        _serverSocketJSON.SendData((new Hashtable {
          {
              "BackgroundColor", this.BackgroundColor
          },
          {
              "ForegroundColor", this.ForegroundColor
          },
          {
              "Msg", msg
          }
        }).ToJSON());
      });
      return this;
    }

    public virtual IDebugConsole WriteLine(string msg)
    {
      return this.Write($"{msg}\r\n");
    }
  }
}
